package com.manager.service.impl;


import com.manager.bean.PageInfo;
import com.manager.bean.TimingSQL;
import com.manager.bean.TimingSQLExample;
import com.manager.mapper.TimingSQLMapper;
import com.manager.service.TimingSQLService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("timingSQLService")
public class TimingSQLServiceImpl implements TimingSQLService {
    @Resource
    private TimingSQLMapper timingSQLMapper;

    @Override
    public PageInfo<TimingSQL> findPageInfo(String name, Integer pageIndex, Integer pageSize) {
        PageInfo<TimingSQL> pi = new PageInfo<TimingSQL>();
        pi.setPageIndex(pageIndex);
        pi.setPageSize(pageSize);
        TimingSQLExample timingSQLExample=new TimingSQLExample();
        TimingSQLExample.Criteria criteria=timingSQLExample.createCriteria();
        if(name!=null&&name!=""){
            criteria.andNameLike("%"+name+"%");
        }
        //获取总条数
        Integer totalCount = timingSQLMapper.countByExample(timingSQLExample);
        if (totalCount>0){
            pi.setTotalCount(totalCount);
            //每一页显示学生信息数
            //currentPage = (pageIndex-1)*pageSize  当前页码数减1*最大条数=开始行数
            List<TimingSQL> timingSQLs =	timingSQLMapper.selectByExample(timingSQLExample);
            int start=(pi.getPageIndex()-1)*pi.getPageSize();
            int end=start+pi.getPageSize();
            if(end>timingSQLs.size()-1){
                end=timingSQLs.size();
            }
            timingSQLs=timingSQLs.subList(start,end);
            pi.setList(timingSQLs);
        }
        return pi;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TimingSQL queryById(Integer id) {
        return this.timingSQLMapper.selectByPrimaryKey(id);
    }


    @Override
    public List<TimingSQL> getAll() {
        TimingSQLExample timingSQLExample = new TimingSQLExample();
        TimingSQLExample.Criteria criteria = timingSQLExample.createCriteria();
        return timingSQLMapper.selectByExample(timingSQLExample);
    }

    /**
     * 新增数据
     *
     * @param timingSQL 实例对象
     * @return 实例对象
     */
    @Override
    public TimingSQL insert(TimingSQL timingSQL) {
        this.timingSQLMapper.insert(timingSQL);
        return timingSQL;
    }

    /**
     * 修改数据
     *
     * @param timingSQL 实例对象
     * @return 实例对象
     */
    @Override
    public TimingSQL update(TimingSQL timingSQL) {
        this.timingSQLMapper.updateByPrimaryKey(timingSQL);
        return this.queryById(timingSQL.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.timingSQLMapper.deleteByPrimaryKey(id) > 0;
    }
}
